//
//  ViewController.m
//  LKDownLoadManagerDemo
//
//  Created by 王郑 on 2018/4/6.
//  Copyright © 2018年 王郑. All rights reserved.
//

#import "ViewController.h"
#import "LKDownLoadManager.h"


@import WebKit;



NSString * const downloadURLString1 = @"http://yxfile.idealsee.com/31f9a479a9c2189bb3ee6e5c581d2026_mp4.mp4";
NSString * const downloadURLString2 = @"http://yxfile.idealsee.com/9f6f64aca98f90b91d260555d3b41b97_mp4.mp4";
NSString * const downloadURLString3 = @"http://yxfile.idealsee.com/d3c0d29eb68dd384cb37f0377b52840d_mp4.mp4";

NSString * const downloadName1 = @"你的名字.mp4";
NSString * const downloadName2 = @"Parlophone.mp4";
NSString * const downloadName3 = @"进击的巨人.mp4";

// 这个文件给了个预估的大小
NSInteger const fileSize1 = 12 * 1000 * 1000;
NSInteger const fileSize2 = 24 * 1000 * 1000;
NSInteger const fileSize3 = 20 * 1000 * 1000;

@interface ViewController ()<WKNavigationDelegate>
@property (weak, nonatomic) IBOutlet UIView *downView1;
@property (weak, nonatomic) IBOutlet UIView *downView2;
@property (weak, nonatomic) IBOutlet UIView *downView3;

@property (weak, nonatomic) IBOutlet UIView *showVIdeoView;
@property (strong, nonatomic) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"沙盒:\n%@",NSHomeDirectory());
    [[LKDownLoadManager sharedManager] setMaxConcurrentCount:2];
    UIBarButtonItem *resumeAllItem = [[UIBarButtonItem alloc] initWithTitle:@"全部恢复" style:(UIBarButtonItemStyleDone) target:self action:@selector(resumeAll)];
    self.navigationItem.rightBarButtonItem = resumeAllItem;
    
    UIBarButtonItem *cancelAllItem = [[UIBarButtonItem alloc] initWithTitle:@"全部暂停" style:(UIBarButtonItemStyleDone) target:self action:@selector(pauseAll)];
    self.navigationItem.leftBarButtonItem = cancelAllItem;
    
    
    
    [self setUpDownView1];
    [self setUpDownView2];
    [self setUpDownView3];
    
    self.webView = [[WKWebView alloc] initWithFrame:self.showVIdeoView.bounds];
    self.webView.navigationDelegate = self;
    self.webView.backgroundColor = [UIColor yellowColor];
    self.webView.userInteractionEnabled = YES;
    [self.showVIdeoView addSubview:self.webView];
}



- (void)resumeAll{
    [[LKDownLoadManager sharedManager]resumeAllDownloads];
}

- (void)pauseAll{
    [[LKDownLoadManager sharedManager]suspendAllDownloads];
}

- (void)setUpDownView1{
    
    UIButton *start1 = [self.downView1 viewWithTag:1];
    UIButton *delete = [self.downView1 viewWithTag:2];
    UIProgressView *progressV = [self.downView1 viewWithTag:3];
    progressV.progress = [[LKDownLoadManager sharedManager]fileHasDownloadedProgressOfFileName:downloadName1];
    
    [start1 addTarget:self action:@selector(download:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [delete addTarget:self action:@selector(delete:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)setUpDownView2{
    
    UIButton *start1 = [self.downView2 viewWithTag:11];
    UIButton *delete = [self.downView2 viewWithTag:12];
    UIProgressView *progressV = [self.downView2 viewWithTag:13];
    progressV.progress = [[LKDownLoadManager sharedManager]fileHasDownloadedProgressOfFileName:downloadName2];
    
    [start1 addTarget:self action:@selector(download:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [delete addTarget:self action:@selector(delete:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)setUpDownView3{
    
    UIButton *start1 = [self.downView3 viewWithTag:21];
    UIButton *delete = [self.downView3 viewWithTag:22];
    UIProgressView *progressV = [self.downView3 viewWithTag:23];
    progressV.progress = [[LKDownLoadManager sharedManager]fileHasDownloadedProgressOfFileName:downloadName3];
    
    [start1 addTarget:self action:@selector(download:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [delete addTarget:self action:@selector(delete:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)download:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    UIProgressView *progressV;
    NSURL *url;
    NSString *fileName;
    NSInteger fileSize = 0;
    switch (tag) {
        case 1:
            progressV = [self.downView1 viewWithTag:3];
            url = [NSURL URLWithString:downloadURLString1];
            fileName = downloadName1;
            fileSize = fileSize1;
            break;
        case 11:
            progressV = [self.downView2 viewWithTag:13];
            url = [NSURL URLWithString:downloadURLString2];
            fileName = downloadName2;
            fileSize = fileSize2;
            break;
        case 21:
            progressV = [self.downView3 viewWithTag:23];
            url = [NSURL URLWithString:downloadURLString3];
            fileName = downloadName3;
            fileSize = fileSize3;
            break;
    }

    __weak typeof(self) wself = self;
    if ([sender.currentTitle isEqualToString:@"开始"]){
        [[LKDownLoadManager sharedManager] download:url filename:fileName filesize:fileSize state:^(LKDownLoadState state) {
            
            [sender setTitle:[wself titleWithDownloadState:state] forState:UIControlStateNormal];
            
        } progress:^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress) {
            
            progressV.progress = progress;
            
        } completion:^(BOOL isSuccess, NSString *filePath, NSError *error) {
            if (isSuccess) {
                NSLog(@"FilePath: %@", filePath);
            } else {
                NSLog(@"Error: %@", error);
            }
        }];
    }else if ([sender.currentTitle isEqualToString:@"等待"]) {
        [[LKDownLoadManager sharedManager] cancelDownloadOfFileName:fileName];
    } else if ([sender.currentTitle isEqualToString:@"暂停"]) {
        [[LKDownLoadManager sharedManager] suspendDownloadOfFileName:fileName];
    } else if ([sender.currentTitle isEqualToString:@"继续"]) {
        [[LKDownLoadManager sharedManager] resumeDownloadOfFileName:fileName];
    } else if ([sender.currentTitle isEqualToString:@"完成"]) {
        NSLog(@"File has been downloaded! It's path is: %@", [[LKDownLoadManager sharedManager] fileFullPathOfFileName:fileName]);
    }
    
    
}

- (void)delete:(UIButton *)sender{
    NSInteger tag = sender.tag;
    UIProgressView *progressV;
    UIButton *firstBtn;
    NSString *fileName;
    switch (tag) {
        case 2:
            progressV = [self.downView1 viewWithTag:3];
            firstBtn = [self.downView1 viewWithTag:1];
            fileName = downloadName1;
            break;
        case 12:
            progressV = [self.downView2 viewWithTag:13];
            firstBtn = [self.downView2 viewWithTag:11];
            fileName = downloadName2;
            break;
        case 22:
            progressV = [self.downView3 viewWithTag:23];
            firstBtn = [self.downView3 viewWithTag:21];
            fileName = downloadName3;
            break;
    }
    [firstBtn setTitle:@"开始" forState:UIControlStateNormal];
    progressV.progress = 0;
    [[LKDownLoadManager sharedManager] deleteFile:fileName];
}


- (NSString *)titleWithDownloadState:(LKDownLoadState)state {
    
    switch (state) {
        case LKDownLoadStateWaiting:
            return @"等待";
        case LKDownLoadStateRunning:
            return @"暂停";
        case LKDownLoadStateSuspended:
            return @"继续";
        case LKDownLoadStateCanceled:
            return @"开始";
        case LKDownLoadStateCompleted:
            return @"完成";
        case LKDownLoadStateFailed:
            return @"开始";
    }
}










#pragma mark - UIWebView
- (IBAction)playVideoByWebView:(id)sender {
    // 加载网络:
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:downloadURLString1]]];
    
    // 加载本地:
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        NSString *path = [[LKDownLoadManager sharedManager] fileFullPathOfFileName:downloadName1];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSString *basePath = [[LKDownLoadManager sharedManager] fileFullPathOfFileName:@""];
        NSURL *baseUrl = [NSURL fileURLWithPath:basePath];
        [self.webView loadFileURL:url allowingReadAccessToURL:baseUrl];
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"完成");
}
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"文件加载失败");
    
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"失败");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
